<?php

class PersonTypeController extends AppController {

	var $paginate = array(
		'PersonType' => array('limit' => 10)
	);

	public function index() {
		$this->PersonType->recursive = 0;
		$this->set('personTypes', $this->paginate());
	}

	public function view($id = null) {
		$this->PersonType->id = $id;
		if (!$this->PersonType->exists()) {
			$this->setFlashError('Záznam nebol nájdený.');
			$this->redirect(array('action' => 'index'));
		}
		$this->set('personType', $this->PersonType->read(null, $id));
	}

	public function add() {
		if ($this->request->is('post')) {
			$this->PersonType->create();
			$this->request->data['PersonType']['commission'] = intval($this->request->data['PersonType']['commission']);
			if ($this->PersonType->save($this->request->data)) {
				$this->setFlashSuccess('Záznam bol uložený do databázy.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->setFlashError('Záznam sa nepodarilo uložiť do databázy.');
			}
		}
	}

	public function edit($id = null) {
		$this->PersonType->id = $id;
		if (!$this->PersonType->exists()) {
			$this->setFlashError('Záznam nebol nájdený.');
			$this->redirect(array('action' => 'index'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->request->data['PersonType']['commission'] = intval($this->request->data['PersonType']['commission']);
			if ($this->PersonType->save($this->request->data)) {
				$this->setFlashSuccess('Záznam bol uložený do databázy.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->setFlashError('Záznam sa nepodarilo uložiť do databázy.');
			}
		} else {
			$this->request->data = $this->PersonType->read(null, $id);
		}
	}

	public function delete($id = null) {
		$this->PersonType->id = $id;
		if (!$this->PersonType->exists()) {
			$this->setFlashError('Záznam nebol nájdený.');
		} else {
			if ($this->PersonType->delete()) {
				$this->setFlashSuccess('Záznam bol zmazaný z databázy.');
			} else {
				$errors = array_merge(array('Záznam nebol zmazaný z databázy.'), Errors::getErrors());
				$message = implode("<br />", $errors);
				$this->setFlashError($message);
			}
		}
		$this->redirect(array('action' => 'index'));
	}
}

?>